#include "jizhi_gnis.h"


#if 0

#define GNIS_HEAD1	0XAC
#define GNIS_HEAD2	0XCA
#define GNIS_END1	0XAA
#define GNIS_END2	0X55
#define GNIS_DATA_LENS	59
#define GNIS_DATA_TYPE	0X60
#define GNIS_GET_DATA_LENS  55//(GNIS_DATA_LENS - 4)


typedef enum {
    ParseStateWaitHead1 = 0, // 0XAC
	ParseStateWaitHead2, // 0XCA
    ParseStateWaiteData,
    ParseStateWaitEnd1,
    ParseStateWaitEnd2
} ParseStateType;



static ParseStateType g_status = ParseStateWaitHead1;
static int g_dataCount = 0;
uint8_t g_data[128];

bool ProtocolParseChar(GnisProtocolType *recvMessage, uint8_t c)
{
	switch(g_status)
	{
		case ParseStateWaitHead1:
		{
			if(c == GNIS_HEAD1)
			{
				g_dataCount = 0;
				g_status = ParseStateWaitHead2;
			}
			break;
		}

		case ParseStateWaitHead2:
		{
			if(c == GNIS_HEAD2)
			{
				g_status = ParseStateWaiteData;
			}
			else
			{
				g_status = ParseStateWaitHead1;
			}
			break;
		}

		case ParseStateWaiteData:
		{
			g_data[g_dataCount++] = c;
			if(g_dataCount == GNIS_GET_DATA_LENS)
			{
				g_status = ParseStateWaitEnd1;
			}
			break;
		}

		case ParseStateWaitEnd1:
		{
			if(c == GNIS_END1)
			{
				g_status = ParseStateWaitEnd2;
			}
			else
			{
				g_status = ParseStateWaitHead1;
			}
			break;
		}

		case ParseStateWaitEnd2:
			g_status = ParseStateWaitHead1;

			if(c == GNIS_END2)
			{
				GnisProtocolType tmp;
				memcpy(&tmp, g_data, GNIS_GET_DATA_LENS);

				//check	msg type and lens
				if(tmp.m_dataLens == GNIS_DATA_LENS && tmp.m_dataType == GNIS_DATA_TYPE)
				{
					memcpy(recvMessage, &tmp, sizeof(GnisProtocolType));
					return true;
				}
			}
			break;
	}

	return false;
}

#else
#define GNIS_HEAD1	0X55
#define GNIS_HEAD2	0XAA
#define GNIS_GET_DATA_LENS  22

typedef enum {
    ParseStateWaitHead1 = 0, // 0X55
	ParseStateWaitHead2, // 0Xaa
    ParseStateWaiteData,
    ParseStateWaitCrc
} ParseStateType;

static ParseStateType g_status = ParseStateWaitHead1;
static int g_dataCount = 0;
static uint8_t g_crc = 0;
static uint8_t g_data[128];


bool ProtocolParseChar(GnisProtocolType *recvMessage, uint8_t c)
{
	switch(g_status)
	{
		case ParseStateWaitHead1:
		{
			g_crc = GNIS_HEAD1;
			if(c == GNIS_HEAD1)
			{
				g_dataCount = 0;
				g_status = ParseStateWaitHead2;
			}
			break;
		}

		case ParseStateWaitHead2:
		{
			g_crc += GNIS_HEAD2;
			if(c == GNIS_HEAD2)
			{
				g_status = ParseStateWaiteData;
			}
			else
			{
				g_status = ParseStateWaitHead1;
			}
			break;
		}

		case ParseStateWaiteData:
		{
			g_crc += c;
			g_data[g_dataCount++] = c;
			if(g_dataCount == GNIS_GET_DATA_LENS)
			{
				g_status = ParseStateWaitCrc;
			}
			break;
		}

		case ParseStateWaitCrc:
		{
			g_status = ParseStateWaitHead1;
			if(c == g_crc)
			{
				memcpy(recvMessage, g_data, GNIS_GET_DATA_LENS);
				return true;
			}

			break;
		}
	}

	return false;
}

#endif
